<template>
  <div class="data-container">
    <h1>{{ areaName }}人口详情</h1>
    
    <div v-if="loading" class="loading">数据加载中...</div>
    
    <div v-else class="data-content">
      <div class="back-button" @click="goBack">
        <el-icon><ArrowLeft /></el-icon> 返回地图
      </div>
      
      <div class="data-grid">
        <div class="data-card">
          <h3>常住人口</h3>
          <p class="value">{{ data.resident || 0 }} 人</p>
        </div>
        
        <div class="data-card">
          <h3>流动人口</h3>
          <p class="value">{{ data.floating || 0 }} 人</p>
        </div>
        
        <div class="data-card">
          <h3>重点人口</h3>
          <p class="value">{{ data.key || 0 }} 人</p>
        </div>
      </div>
      
      <div class="chart-container">
        <div id="populationChart" style="width: 100%; height: 400px;"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import * as echarts from 'echarts';
import { ArrowLeft } from '@element-plus/icons-vue';
import axios from 'axios';

const router = useRouter();
const route = useRoute();
const areaName = ref(route.query.area || '');
const data = ref({ resident: 0, floating: 0, key: 0 });
const loading = ref(true);

onMounted(async () => {
  if (areaName.value) {
    await fetchData();
    initChart();
  }
});

async function fetchData() {
  try {
    loading.value = true;
    const response = await axios.post(
      'http://localhost:8080/api/population/queryByAdress',
      { area: areaName.value }
    );
    
    if (response.data.code === 1) {
      const result = response.data.result || {};
      data.value = {
        resident: result[1] || 0,
        floating: result[2] || 0,
        key: result[3] || 0
      };
    }
  } catch (error) {
    console.error('获取数据失败:', error);
  } finally {
    loading.value = false;
  }
}

function initChart() {
  const chartDom = document.getElementById('populationChart');
  if (!chartDom) return;
  
  const myChart = echarts.init(chartDom);
  const option = {
    title: {
      text: `${areaName.value}人口分布`,
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '人口数量',
        type: 'pie',
        radius: '50%',
        data: [
          { value: data.value.resident, name: '常住人口' },
          { value: data.value.floating, name: '流动人口' },
          { value: data.value.key, name: '重点人口' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };
  
  myChart.setOption(option);
  window.addEventListener('resize', () => myChart.resize());
}

function goBack() {
  router.go(-1);
}
</script>

<style scoped>
.data-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

h1 {
  color: #333;
  text-align: center;
  margin-bottom: 30px;
}

.loading {
  text-align: center;
  font-size: 18px;
  color: #666;
  padding: 50px;
}

.back-button {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  background-color: #f5f7fa;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 20px;
  transition: all 0.3s;
}

.back-button:hover {
  background-color: #e4e7ed;
}

.data-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 30px;
}

.data-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  text-align: center;
}

.data-card h3 {
  color: #666;
  margin-bottom: 10px;
}

.data-card .value {
  font-size: 24px;
  font-weight: bold;
  color: #409eff;
}

.chart-container {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>